package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Dict;
import com.atguigu.result.Result;
import com.atguigu.service.DictService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/10 9:57 周五
 * description:
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {
    private final static String PAGE_INDEX = "dict/index";

    @Reference
    private DictService dictService;

    /**
     * 返回二手房管理页面
     *
     * @return
     */
    @RequestMapping
    public String index() {
        return PAGE_INDEX;
    }

    /**
     * 返回json数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/findZnodes")
    @ResponseBody
    public Result findByParentId(
            //解决前后端参数不一致的问题
            //defaultValue：当前形参设置默认值
            @RequestParam(value = "id", defaultValue = "0") Long id) {
        List<Map<String, Object>> zNodes = dictService.findZnodes(id);
        return Result.ok(zNodes);
    }

    //根据市找到下面的所有区
    @RequestMapping(value = "findListByParentId/{parentId}")
    @ResponseBody
    public Result<List<Dict>> findListByParentId(
            @PathVariable Long parentId
    ) {
        List<Dict> list = dictService.findListByParentId(parentId);
        return Result.ok(list);
    }

    @RequestMapping(value = "findListByDictCode/{dictCode}")
    @ResponseBody
    public Result<List<Dict>> findListByDictCode(String dictCode) {
        List<Dict> list = dictService.findListByDictCode(dictCode);
        return Result.ok(list);
    }
}
